package com.yunhe.policyfulfillment.policyful.controller;

import com.github.pagehelper.PageInfo;
import com.yunhe.common.model.ResultObject;
import com.yunhe.common.model.mybatis.PageResult;
import com.yunhe.policyfulfillment.policyful.domain.PolicyInfo;
import com.yunhe.policyfulfillment.policyful.model.PolicyInfoQueryCondition;
import com.yunhe.policyfulfillment.policyful.service.PolicyInfoService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;

import java.text.ParseException;
import java.util.List;


@Api(value = "PolicyInfoController", description = "政策配置", tags = {"Model_PolicyInfo"})
@RestController
@RequestMapping("/policyInfo")
public class PolicyInfoController {

    @Autowired
    private PolicyInfoService policyInfoService;

    @ApiOperation("新增政策")
    @PostMapping(value = "/create")
    public ResponseEntity<ResultObject<Integer>> create(@RequestBody PolicyInfo policyInfo) throws ParseException {
        int id = policyInfoService.insert(policyInfo);
        return new ResponseEntity<>(new ResultObject<>(id), HttpStatus.OK);
    }

    @ApiOperation("根据id查看政策")
    @GetMapping(value = "/getById")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "政策ID", required = true, dataType = "Long", paramType = "query")
    })
    public ResponseEntity<ResultObject<PolicyInfo>>  getById(@RequestParam(value = "id", required = true) Long id){
        PolicyInfo result = policyInfoService.selectByPrimaryKey(id);
        return new ResponseEntity<>(new ResultObject<>(result), HttpStatus.OK);
    }

    @ApiOperation("根据id修改政策")
    @PostMapping(value = "/updateById")
    public ResponseEntity<ResultObject<String>>  updateById(@RequestBody PolicyInfo policyInfo) throws ParseException {
        policyInfoService.updateByPrimaryKeySelective(policyInfo);
        return new ResponseEntity<>(new ResultObject<>("success"), HttpStatus.OK);
    }

    @ApiOperation("根据条件获取政策分页（后台）")
    @PostMapping(value = "/getPageByCondition")
    public ResponseEntity<ResultObject<List<PolicyInfo>>>  getPageByCondition(@RequestBody PolicyInfoQueryCondition condition){
        PageResult result = policyInfoService.selectByConditionForPage(condition);
        return new ResponseEntity<>(new ResultObject(result), HttpStatus.OK);
    }

    @ApiOperation(value = "上线/下线操作",
            notes = "上线带上发布时间(releaseTime)，下线不用带上时间")
    @PostMapping(value = "/onlineOrOffline")
    public ResponseEntity<ResultObject<String>>  onlineOrOffline(@RequestBody PolicyInfo policyInfo){
        policyInfoService.onlineOrOffline(policyInfo);
        return new ResponseEntity<>(new ResultObject<>("success"), HttpStatus.OK);
    }

    @ApiOperation("批量下线操作")
    @PostMapping(value = "/patchOffline")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "政策Ids", required = true, dataType = "String", paramType = "query")
    })
    public ResponseEntity<ResultObject<String>>  patchOffline(@RequestParam(value = "ids", required = true) String ids){
        policyInfoService.patchOffline(ids);
        return new ResponseEntity<>(new ResultObject<>("success"), HttpStatus.OK);
    }

    @ApiOperation("删除政策")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "政策Ids", required = true, dataType = "String", paramType = "query")
    })
    @DeleteMapping(value = "/deleteByIds")
    public ResponseEntity<ResultObject<String>>  deleteByIds(@RequestParam(value = "ids", required = true) String ids){
        policyInfoService.deleteByIds(ids);
        return new ResponseEntity<>(new ResultObject<>("success"), HttpStatus.OK);
    }

}